<?php

set_include_path(implode(PATH_SEPARATOR,
    array(
        get_include_path(),
        '..',
        dirname(dirname(dirname(__FILE__)))
    )));

require_once 'components/utils/debug_utils.php';

Warning::BeginSuppress(PHPWarning::$Deprecated);
Warning::BeginSuppress(PHPWarning::$Strict);

require_once 'tests/simpletest/autorun.php';

require_once 'database_engine/condition.php';

class ConditionBuilderTests extends UnitTestCase
{
    private $conditionImp;

    public function setUp()
    {
        $this->conditionImp = new GenericConditionImp();
    }

    public function testSimpleBinaryCondition()
    {
        $condition = new BinaryCondition(
            Literal::Number(1),
            BinaryOperator::$Equals,
            Literal::Number(2)
        );
        
        $this->assertEqual('1 = 2',
            $condition->AsSql($this->conditionImp)
        );
    }

    public function testFieldBinaryCondition()
    {
        $condition = new BinaryCondition(
            FieldName::Short('field'),
            BinaryOperator::$Equals,
            Literal::Number(2)
        );

        $this->assertEqual('field = 2',
            $condition->AsSql($this->conditionImp)
        );
    }

    public function testFullFieldBinaryCondition()
    {
        $condition = new BinaryCondition(
            FieldName::Full('t0', 'field'),
            BinaryOperator::$Equals,
            Literal::Number(2)
        );

        $this->assertEqual('t0.field = 2',
            $condition->AsSql($this->conditionImp)
        );
    }

    public function testUnaryCondition()
    {
        $condition = new UnaryCondition(
            UnaryOperator::$Not,
            Literal::Boolean(true)
        );

        $this->assertEqual('NOT TRUE',
            $condition->AsSql($this->conditionImp)
        );
    }

    public function testSimpleComplexCondition()
    {
        $condition = new MultiCondition(
            MultiOperator::$And,
            array(
                Literal::Boolean(true),
                Literal::Boolean(false)
            )
        );

        $condition->Add(Literal::Boolean(true));

        $this->assertEqual('(TRUE) AND (FALSE) AND (TRUE)',
            $condition->AsSql($this->conditionImp)
        );
    }

    public function testComplexConditionWithRequiredBrackets()
    {
        $condition = new MultiCondition(
            MultiOperator::$And,
            array(
                new MultiCondition(MultiOperator::$And, array(
                    Literal::Boolean(false),
                    Literal::Boolean(true)
                )),
                Literal::Boolean(false)
            )
        );

        $condition->Add(Literal::Boolean(true));

        $this->assertEqual('((FALSE) AND (TRUE)) AND (FALSE) AND (TRUE)',
            $condition->AsSql($this->conditionImp)
        );
    }
}

?>